/**
* 系统管理 定时任务
*/
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>定时任务</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="任务名称">
        <el-input v-model="formInline.serchData.jobName" placeholder="请输入任务名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="方法名称">
        <el-input v-model="formInline.serchData.methodName" placeholder="请输入方法名称" size="small"></el-input>
      </el-form-item>
      <el-form-item label="任务状态">
        <el-select v-model="formInline.roleId" placeholder="请选择" size="small">
          <el-option label="请选择" selected value="0"></el-option>
          <el-option label="正常" value="2"></el-option>
          <el-option label="暂停" value="4"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="small" type="primary" @click="search">查询</el-button>
        <el-button icon="el-icon-plus" size="small" type="primary" @click="handleEdit()">重置</el-button>
      </el-form-item>
    </el-form>
    <!--    TODO（新建任务还未开发完毕）-->
    <el-button icon="el-icon-search" size="small" type="primary" @click="search">新增任务</el-button>

    <!--列表-->
    <el-table v-loading="loading" :data="listData" border element-loading-text="拼命加载中" highlight-current-row
              size="small" style="width: 100%;" @selection-change="selectChange">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column label="任务ID" prop="jobId" sortable width="300">
      </el-table-column>
      <el-table-column label="任务名称" prop="jobName" sortable width="300">
      </el-table-column>
      <el-table-column label="执行表达式" prop="cronExpression" sortable width="300">
      </el-table-column>
      <el-table-column label="状态" prop="status" sortable width="100">
      </el-table-column>
      <el-table-column label="备注" prop="remark" sortable width="100">
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" sortable width="300">
      </el-table-column>

      <el-table-column align="center" label="操作" min-width="300">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">执行</el-button>
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">恢复</el-button>
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button size="mini" type="danger" @click="deleteUser(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 修改界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="120px">
        <el-form-item label="任务名称" prop="permissionName">
          <el-input v-model="editForm.permissionName" auto-complete="off" placeholder="权限名称" size="small"></el-input>
        </el-form-item>
        <el-form-item label="任务组名" prop="permission">
          <el-input v-model="editForm.permission" auto-complete="off" placeholder="权限CODE" size="small"></el-input>
        </el-form-item>
        <el-form-item label="方法名称" prop="permission">
          <el-input v-model="editForm.permission" auto-complete="off" placeholder="权限CODE" size="small"></el-input>
        </el-form-item>
        <el-form-item label="方法参数" prop="permission">
          <el-input v-model="editForm.permission" auto-complete="off" placeholder="权限CODE" size="small"></el-input>
        </el-form-item>
        <el-form-item label="cron表达式" prop="permission">
          <el-input v-model="editForm.permission" auto-complete="off" placeholder="权限CODE" size="small"></el-input>
        </el-form-item>
        <el-radio-group v-model="radio" lable="执行错误策略">
          <el-radio :label="3">默认</el-radio>
          <el-radio :label="6">立即执行</el-radio>
          <el-radio :label="9">执行一次</el-radio>
          <el-radio :label="9">放弃执行</el-radio>
        </el-radio-group>
        <el-radio-group v-model="radio" lable="状态">
          <el-radio :label="3">正常</el-radio>
          <el-radio :label="6">暂停</el-radio>
        </el-radio-group>
        <el-input
          v-model="textarea"
          :label="备注"
          :rows="2"
          placeholder=""
          type="textarea">
        </el-input>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button :loading="loading" class="title" size="small" type="primary" @click="submitForm('editForm')">保存
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {ermissionDelete, ermissionSave, roleDropDown, RolePermission} from '../../api/userMG'
import Pagination from '../../components/Pagination'
import {listJob} from "../../api/sysconfigMG";

export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      title: '添加',
      editForm: {
        permissionId: '',
        permissionName: '',
        permission: '',
        token: localStorage.getItem('token')
      },
      // rules表单验证
      rules: {
        permissionName: [
          {required: true, message: '请输入权限名称', trigger: 'blur'}
        ],
        permission: [
          {required: true, message: '请输入权限CODE', trigger: 'blur'}
        ]
      },
      formInline: {
        pageNum: 1,
        pageSize: 10,
        pageCount: 10,
        serchData: {
          jobName: '',
          methodName: '',
          status: ''
        }
      },
      // 选择数据
      selectdata: [],
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  watch: {},

  /**
   * 创建完毕
   */

  created() {
    this.getdata(this.formInline)
    this.getAccsee()
  },

  /**
   * 里面的方法只有被调用才会执行
   */

  methods: {
    // 获取数据方法
    getdata(parameter) {
      this.loading = true

      /***
       * 调用接口，注释上面模拟数据 取消下面注释
       */
      // 获取权限列表
      listJob(parameter)
        .then(res => {
          this.loading = false
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.listData = res.result.list
            // 分页赋值
            this.pageparm.currentPage = this.formInline.page
            this.pageparm.pageSize = this.formInline.limit
            this.pageparm.total = res.count
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('权限管理列表获取失败，请稍后再试！')
        })
    },
    // 获取权限
    getAccsee() {
      roleDropDown()
        .then(res => {
          if (res.success == false) {
            this.$message({
              type: 'info',
              message: res.msg
            })
          } else {
            this.userparm = res.data
          }
        })
        .catch(err => {
          this.$message.error('权限获取失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.limit = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.editFormVisible = true
      // 深拷贝并赋值
      //this.editForm = Object.assign({}, row)
      if (row != undefined && row != 'undefined') {
        this.title = '修改'
        this.editForm.permissionId = row.permissionId
        this.editForm.permissionName = row.permissionName
        this.editForm.permission = row.permission
      } else {
        this.title = '添加'
        this.editForm.permissionId = ''
        this.editForm.permissionName = ''
        this.editForm.permission = ''
      }
    },
    // 编辑、增加页面保存方法
    submitForm(editData) {
      this.$refs[editData].validate(valid => {
        if (valid) {
          ermissionSave(this.editForm)
            .then(res => {
              this.editFormVisible = false
              this.loading = false
              if (res.success) {
                this.getdata(this.formInline)
                this.$message({
                  type: 'success',
                  message: '权限管理保存成功！'
                })
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.editFormVisible = false
              this.loading = false
              this.$message.error('权限管理保存失败，请稍后再试！')
            })
        } else {
          return false
        }
      })
    },
    // 删除权限
    deleteUser(index, row) {
      this.$confirm('确定要删除吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          ermissionDelete(row.permissionId)
            .then(res => {
              if (res.success) {
                this.$message({
                  type: 'success',
                  message: '权限管理已删除！'
                })
                this.getdata(this.formInline)
              } else {
                this.$message({
                  type: 'info',
                  message: res.msg
                })
              }
            })
            .catch(err => {
              this.loading = false
              this.$message.error('权限管理删除失败,请稍后再试！')
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 选择复选框事件
    selectChange(val) {
      this.selectdata = val
    },
    // 配置权限
    RolePermission() {
      let parms = {
        roleId: '',
        permissionIds: ''
      }
      if (this.formInline.roleId == '0') {
        this.$message({
          type: 'info',
          message: '请选择角色'
        })
        return false
      }
      parms.roleId = this.formInline.roleId
      let len = this.selectdata
      let ids = []
      if (len != 0) {
        for (let i = 0; i < len.length; i++) {
          ids.push(len[i].permissionId)
        }
      }
      parms.permissionIds = ids.join(',')
      RolePermission(parms)
        .then(res => {
          if (res.success) {
            this.$message({
              type: 'success',
              message: '配置权限成功！'
            })
            this.getdata(this.formInline)
          } else {
            this.$message({
              type: 'info',
              message: res.msg
            })
          }
        })
        .catch(err => {
          this.loading = false
          this.$message.error('配置权限失败,请稍后再试！')
        })
    },
    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}

.userRole {
  width: 100%;
}
</style>

